package com.aerors.DataProcess;

import java.util.Vector;

import org.gdal.gdal.Dataset;
import org.gdal.gdal.Driver;
import org.gdal.gdal.WarpOptions;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.gdal.ogr.ogr;

public class RasterClip {

	public static void main(String[] args) {
		gdal.AllRegister();
		ogr.RegisterAll();

		String inTiff = "C:\\data\\cogeo\\2017cogeo.tif";
		String inShp = "C:\\data\\shapes\\乡shp\\称多县乡界.shp";
		String outTiff = "C:\\data\\cogeo\\testclip.tif";

		Dataset src = gdal.Open(inTiff);

		Dataset[] src_array = { src };

		Vector<String> options = new Vector<>();
		options.add("-of");
		options.add("GTiff");
		options.add("-cutline");
		options.add(inShp);
		options.add("-crop_to_cutline");
		options.add("-dstalpha");
		WarpOptions warpOptions = new WarpOptions(options);

		Dataset warp = gdal.Warp("warp.vrt", src_array, warpOptions);
		
		//统计
		int bandCount = warp.getRasterCount();
		int width = warp.GetRasterBand(1).getXSize();
		int height = warp.GetRasterBand(1).getYSize();
		
		int blockX = warp.GetRasterBand(1).GetBlockXSize();
		int blockY = warp.GetRasterBand(1).GetBlockYSize();
		
		System.out.println("blockX:" + blockX);
		System.out.println("blockY:" + blockY);
		//逐行扫描统计
		for(int i=0;i<height;i++) {
			int[] lineBuffer = new int[width * bandCount];
			warp.ReadRaster(0, 0, width, 1, width, 1,gdalconst.GDT_Int32, lineBuffer, new int[] {1,2,3});
			
			System.out.println(lineBuffer);
		}
		
//		Driver tiffDriver = gdal.GetDriverByName("GTiff");
//		Dataset warp_out = tiffDriver.CreateCopy(outTiff, warp);

		src.delete();
		warp.delete();
//		warp_out.delete();
	}
}
